package com.xpf.controller;

import com.xpf.pojo.Orders;
import com.xpf.pojo.Users;
import com.xpf.pojo.vo.UserVO;
import com.xpf.service.center.MyOrdersService;
import com.xpf.utils.JSONResult;
import com.xpf.utils.RedisOperator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.util.UUID;

@Controller
public class BaseController {

    @Autowired
    private RedisOperator redisOperator;

    public static final String FOODIE_SHOPCART = "shopcart";

    public static final Integer COMMON_PAGE_SIZE = 10;
    public static final Integer PAGE_SIZE = 20;

    public static final String REDIS_USER_TOKEN = "redis_user_token";
    // 支付中心的调用地址
    String paymentUrl = "http://payment.t.mukewang.com/foodie-payment/payment/createMerchantOrder";		// produce

    //微信支付成功 -> 支付中心 -> 天天吃货平台
    //                     |-> 回调通知的url  （注：此地址需要范围本地的端口，这里使用了内网穿透工具127.0.0.1:8088）
    String payReturnUrl = "http://gcyx3r.natappfree.cc/orders/notifyMerchantOrderPaid";

    //头像上传的位置
    // D:\src\photo这里的File.separator根据操作系统对的不同，在linux和mac中是指/，在windows中\\
    public static final String IMAGE_USER_FACE_LOCATION = "D:"+ File.separator+"src"+File.separator+"photo";
//    public static final String IMAGE_USER_FACE_LOCATION = "D:\\src\\photo";

    /**
     * 用于验证用户和订单是否有关联关系，避免非法用户调用
     * @return
     */
    @Autowired
    public MyOrdersService myOrdersService;

    public JSONResult checkUserOrder(String userId, String orderId){
        Orders order = myOrdersService.queryMyOrder(userId, orderId);
        if (order == null){
            return JSONResult.errorMsg("订单不存在!");
        }
        return JSONResult.ok(order);
    }

    /**
     * //生成用户token，存入redis会话。记录用户登录(实现用户redis会话)
     * @param user
     * @return
     */
    public UserVO conventUserVO(Users user){
        String uniqueToken = UUID.randomUUID().toString().trim();
        redisOperator.set(REDIS_USER_TOKEN + ":"+ user.getId(), uniqueToken);

        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        userVO.setUserUniqueToken(uniqueToken);
        return userVO;
    }

}
